package com.ten.jinxiancun.controller;

import com.ten.jinxiancun.server.impl.menuServerImpl;
import com.ten.jinxiancun.server.impl.rolesServeImpl;
import com.ten.jinxiancun.utils.Result;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/roles")
public class rolesController {
    /*接口注入*/
    @Autowired
    rolesServeImpl rsl;
    @Autowired
    menuServerImpl msl;
    /*角色查询*/
    @RequestMapping("/getRolesAll")
    public Object getRolesAll(@RequestParam Map<String, Object> map) {
        Integer page = Integer.parseInt(map.get("page").toString());
        Integer limit = Integer.parseInt(map.get("limit").toString());
        map.put("limit", limit);
        map.put("page", (page - 1) * limit);
        List<Map<String, Object>> user = rsl.getAllRolesFind(map);
        int count = rsl.getRolesCountFind(map);
        Result res = new Result("0", "true", count, user);
        return res;
    }

    /*添加角色*/
    @RequestMapping("addRoles")
    public  Object  addRoles(@RequestParam Map<String, Object> map){
        int i= rsl.AddRolesFind(map);
        Result res = new Result("0", "true",i, true);
        return res;
    }
    /*修改角色*/
    @RequestMapping("updateRoles")
    public  Object  updateRoles(@RequestParam Map<String, Object> map){
        int i= rsl.UpdateRolesFind(map);
        Result res = new Result("0", "true",i, true);
        return res;
    }
    /*删除角色*/
    @RequestMapping("/deleteRoles")
    public Object deleteRoles(@RequestParam("id") Integer id) {
        int i = rsl.DelectRolesFind(id);
        Result res = new Result("0", "true", i, true);
        return res;
    }

    /*树形图*/
    /*树形图模块*/
    /*模块*/
    @RequestMapping("/getRolesTree")
    public  Object getRolesTree(@RequestParam Map<String,Object> map){
        List<Map<String, Object>> roles= rsl.getRolesMenuFind(map);
        Result res = new Result("0", "操作成功", rsl.rolesCountFind(map), roles);
        System.out.println("==========>" + res);
        return res;
    }
    /*角色*/
    //getRoles
    @RequestMapping("/getMODRoles")
    public Object getMODRoles(@RequestParam Map<String, Object> map) {
        List<Map<String, Object>> roles = rsl.getRolesFind(map);
        Map<String, Object> check = new HashMap<>();
        for (Map<String, Object> r : roles) {
            check.put("type", 0);
            check.put("checked", 0);
            r .put("checkArr", check);
        }
        Result res = new Result("0", "操作成功", msl.countFind(map),roles);
        System.out.println("==========>" + res);
        return res;
    }
    @RequestMapping("/addRolesMenu")
    public Object addRolesCSK(@RequestParam Map<String, Object> m) {
        int i = rsl.addRolesMenuFind(m);
        Result res = new Result("0", "true", i, i);
        return res;
    }

    @RequestMapping("/deleteRolesMenu")
    public Object deleteRoles(@Param("menuId") Integer menuId, @Param("roleId") Integer roleId) {
        int i = rsl.deleteRolesMenuFind(menuId, roleId);
        Result res = new Result("0", "true", i, i);
        return res;
    }
}
